package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.Result;
import com.hhs.storage.bean.YymHuiyuanTc;
import com.hhs.storage.bean.YymUserTcLink;
import com.hhs.storage.service.YymHuiyuanTcService;
import com.hhs.storage.service.YymUserTcLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuanjun
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/do/yymhuiyuantc")
@Slf4j
public class YymHuiyuanTcController {
    @Resource
    YymHuiyuanTcService yymHuiyuanTcService;
    @Resource
    YymUserTcLinkService yymUserTcLinkService;
    @RequestMapping(value = "getAll")
    public Map<String, Object> getAll(InputParam inputParam) throws Exception {
        log.info("查询会员套餐信息");
        EntityWrapper ew = new EntityWrapper<YymHuiyuanTc>();
        ew.orderBy("price_",true);
        Page<YymHuiyuanTc> result = yymHuiyuanTcService.selectPage(new Page<YymHuiyuanTc>(inputParam.getPage(), inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除套餐");
        yymHuiyuanTcService.deleteById(id);
        //会员套餐连接表删除
        yymUserTcLinkService.delete(new EntityWrapper<YymUserTcLink>().eq("tc_id_",id));
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "add")
    public Map<String, Object> saveOrUpdate(YymHuiyuanTc yymHuiyuanTc){
        log.info("新增或插入套餐信息");
        boolean bb=yymHuiyuanTcService.insert(yymHuiyuanTc);
        if(bb==true){
            return Result.sucJsonResp(bb);
        }
        return Result.failJsonResp("新增不成功！");
    }

    @RequestMapping(value = "editRole")
    public Map<String, Object> editRole(YymHuiyuanTc yymHuiyuanTc){
        log.info("更新套餐的关联角色");
        boolean bb=yymHuiyuanTcService.insertOrUpdate(yymHuiyuanTc);
        if(bb==true){
            return Result.sucJsonResp(bb);
        }
        return Result.failJsonResp("修改套餐角色不成功！");
    }
}

